package com.pic.mgt.action;

import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.pic.mgt.log.IgxLog;
import com.pic.mgt.manager.terminal.TerminalManagementOperationException;
import com.pic.mgt.manager.terminal.TerminalManager;
import com.pic.mgt.scheduler.Scheduler;
import com.pic.mgt.scheduler.SchedulerException;

public class TerminalSyncMgtActionImpl implements TerminalSyncMgtAction {
	private TerminalManager terminalManager;
	private Scheduler scheduler;
	private Class syncTerminalExecutionQuartzJobClass;

	@Override
	public boolean addSyncTerminalSchedule(String scheduleName,
			String cronExpression, String scheduleDescrption) {
		if (null == scheduleName || scheduleName.isEmpty() ||
			null == cronExpression || cronExpression.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
			"Adding terminal synchronization schedule failed. Did not provide schedule name or cron expression.");
			// ********************** Log **********************

			return false;
		}

		try {
			this.scheduler.scheduleCronJob(cronExpression, scheduleName,
										   TERMINAL_SYNC_SCHEDULER_GROUPNAME,
										   scheduleDescrption,
										   syncTerminalExecutionQuartzJobClass,
										   Collections.emptyMap());

			return true;
		} catch (SchedulerException e) {
			// 不用寫 Log (已寫)

			return false;
		}
	}

	@Override
	public String getSyncTerminalScheduleInfoByBizName() {
		try {
			// throws SchedulerException
			List<Map<String, Object>> jobInfoMapList =
				this.scheduler.getScheduleJobInfoByGroupName(TERMINAL_SYNC_SCHEDULER_GROUPNAME);
			
			return this.scheduler.formatSchedules(jobInfoMapList);
		} catch (SchedulerException e) {
			// 不用寫 log
			return null;
		}
	}

	@Override
	public boolean modifySyncTerminalSchedule(String scheduleName,
			String cronExpression, String scheduleDescrption) {
		if (null == scheduleName || scheduleName.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
			"Modifying terminal synchronization schedule failed. Did not provide schedule name or cron expression.");
			// ********************** Log **********************

			return false;
		}


		try {
			this.scheduler.rescheduleCronJob(cronExpression, scheduleName,
					TERMINAL_SYNC_SCHEDULER_GROUPNAME,
					scheduleDescrption);

			return true;
		} catch (SchedulerException e) {
			// 不用寫 Log (已寫)

			return false;
		}
	}

	@Override
	public boolean removeSyncTerminalSchedule(String scheduleName) {
		if (null == scheduleName || scheduleName.isEmpty()) {
			// ********************** Log **********************
			IgxLog.messageLog.warn(this,
							"Removing terminal synchroniztion schedule failed. Did not provide schedule name.");
			// ********************** Log **********************
			
			return false;
		}
		
		
		try {
			this.scheduler.removeJob(scheduleName, TERMINAL_SYNC_SCHEDULER_GROUPNAME);
			
			return true;
		} catch (SchedulerException e) {
			// 不用寫 Log (已寫)
			
			return false;
		}
	}

	@Override
	public boolean synchronizeTerminal() {
		try {
			this.terminalManager.synchronizeTerminals();
			
			return true;
		} catch (TerminalManagementOperationException e) {
			// 不用寫 log (已寫)
			return false;
		}
	}
	
	// --- getter and setter ---

	public void setTerminalManager(TerminalManager terminalManager) {
		this.terminalManager = terminalManager;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void setSyncTerminalExecutionQuartzJobClass(
			Class syncTerminalExecutionQuartzJobClass) {
		this.syncTerminalExecutionQuartzJobClass = syncTerminalExecutionQuartzJobClass;
	}

}
